package com.jiangjiang.eshop.logistics.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.logistics.convert.LogisticsFreightTemplateConvert;
import com.jiangjiang.eshop.logistics.dao.FreightTemplateMapper;
import com.jiangjiang.eshop.logistics.domain.FreightTemplateEntity;
import com.jiangjiang.eshop.logistics.dto.FreightTemplateDTO;
import com.jiangjiang.eshop.logistics.dto.FreightTemplateQueryDTO;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author Jianghaowen
 * @Date 2021/9/15
 * @Description: TODO
 * @Version V1.0
 **/
@Service
public class FreightTemplateService extends ServiceImpl<FreightTemplateMapper, FreightTemplateEntity> {
    @Autowired
    private LogisticsFreightTemplateConvert freightTemplateConvert;

    /**
     * 运费模板分页查询
     * @param dto 查询入参
     * @return 查询结果
     */
    public Page<FreightTemplateDTO> queryForPage(FreightTemplateQueryDTO dto){
        Page<FreightTemplateEntity> page = new Page<>(dto.getPageNo(), dto.getPageSize(), true);
        LambdaQueryWrapper<FreightTemplateEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(dto.getId() != null, FreightTemplateEntity::getId, dto.getId())
                .eq(dto.getType() != null, FreightTemplateEntity::getType, dto.getType())
                .like(Strings.isNotEmpty(dto.getName()), FreightTemplateEntity::getName, dto.getName());
        return freightTemplateConvert.convertPage2DTOList(this.getBaseMapper().selectPage(page,wrapper));
    }

    /**
     * 根据运费模板id查询记录
     * @param id 运费模板id
     * @return 查询结果
     */
    public FreightTemplateDTO queryById(Long id){
        return freightTemplateConvert.convertEntity2DTO(this.getById(id));
    }

    /**
     * 根据运费模板id查询记录
     * @param dto 运费模板数据
     * @return 处理结果
     */
    public Boolean createTemplate(FreightTemplateDTO dto){
        return this.save(freightTemplateConvert.convertDTO2Entity(dto));
    }

    /**
     * 运费模板更新
     * @param dto 运费模板数据
     * @return 处理结果
     */
    public Boolean updateTemplate(FreightTemplateDTO dto){
        return this.updateById(freightTemplateConvert.convertDTO2Entity(dto));
    }
}
